package com.itheima.controller;


import com.itheima.pojo.User;
import com.itheima.service.LoginService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/v1/login")
public class LoginController {

    @Autowired
    private LoginService service;


    @PostMapping("/in")
    public ResponseResult login(@RequestBody Map<String,String> map, HttpServletRequest request){
        User user = service.findUserByClassIdMobilePassword(map);

        if (user != null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            return new ResponseResult(0,"操作成功","");
        }

        return new ResponseResult(0,"操作失败");


    }


    @GetMapping("/{mobile}")
    public ResponseResult findRoleByName(@PathVariable String mobile){

        return service.getClassByMobile(mobile);

    }
}
